package javasec.contempt.dialogs;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class KeyImportDialog extends Dialog
{
	public String alias;
	public X509Certificate certificate=null;
	Text aliasText=null;
	
	String stringValue=null;
	String prompt="";
	String windowName="";
	Text stringText=null;
	public boolean aborted=true;
	
	public KeyImportDialog(Shell parent,String wName)
	{
		super(parent);
		windowName=wName;
	}
	
	protected void okPressed()
	{
		aborted=false;
		
		alias=aliasText.getText();
		super.okPressed();
	}
	
	protected Control createDialogArea(Composite parent)
	{
		
		Composite composite = new Composite(parent,0);
		composite.setLayout(new GridLayout(1,false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite aliasComposite=new Composite(composite,0);
		aliasComposite.setLayout(new GridLayout(2,false));
		Label aliasLabel=new Label(aliasComposite,SWT.NONE);
		aliasLabel.setText("alias");
		aliasLabel.pack();
		aliasText=new Text(aliasComposite, SWT.SINGLE|SWT.BORDER);
		aliasText.pack();
		aliasComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
		aliasText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label entryLabel=new Label(aliasComposite,SWT.NONE);
		entryLabel.setText("key");
		entryLabel.pack();
		final Text entryBox=new Text(aliasComposite,SWT.SINGLE|SWT.BORDER);
		//entryBox.setText("");
		final Label certVerify=new Label(composite,SWT.NONE);
		certVerify.setText("warning: invalid key");
		
		entryBox.addModifyListener(new ModifyListener()
		{
			public void modifyText(ModifyEvent e)
			{
				
				try
				{
					String keyString=entryBox.getText();
					byte[] objectBytes=new byte[keyString.length()/2];
					
					for(int x=0;x<objectBytes.length;++x)
					{
						String thisByte=keyString.substring(x*2,x*2+2);
						objectBytes[x]|=(Integer.parseInt(thisByte,16));
					}
					
					ByteArrayInputStream iStream=new ByteArrayInputStream(objectBytes);
					ObjectInputStream oiStream=new ObjectInputStream(iStream);
					Object possibleCert=oiStream.readObject();
					
					if(possibleCert instanceof X509Certificate)
					{
						alias=aliasText.getText();
						certificate=(X509Certificate)possibleCert;
						certVerify.setText("key seems valid");
					}
					else certVerify.setText("warning: invalid key");
				}
				catch(Exception e1)
				{
					certVerify.setText("warning: invalid key");
				}
			}
		});
		entryBox.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		return composite;
	}
	
	protected void configureShell(Shell newShell)
	{
		super.configureShell(newShell);
		newShell.setText(windowName);
	}
}
